package sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Tag(name = "C端-地址簿接口", description = "地址簿相关业务")
@RequestMapping(value = "/user/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @Operation(description = "新增地址", summary = "新增地址")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功新增地址")
    })
    @PostMapping
    public Result save(@RequestBody AddressBook addressBook){
        log.info("新增地址：{}", addressBook);
        addressBookService.add(addressBook);
        return Result.success();
    }

    @Operation(summary = "设置默认地址", description = "设置默认地址")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功设置默认地址")
    })
    @PutMapping("/default")
    public Result setDefault(Long id){
        log.info("设置默认地址：{}", id);
        addressBookService.setDefault(id);
        return Result.success();
    }

    @Operation(summary = "查询默认地址", description = "查询默认地址")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功查询默认地址")
    })
    @GetMapping("/default")
    public Result<AddressBook> getDefault(){
        log.info("查询默认地址");
        AddressBook addressBook = addressBookService.getDefault();
        return Result.success(addressBook);
    }

    @Operation(summary = "查询地址列表", description = "查询地址列表")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功查询地址列表")
    })
    @GetMapping("/list")
    public Result<List<AddressBook>> list(){
        log.info("查询地址列表");
        List<AddressBook> list = addressBookService.list();
        return Result.success(list);
    }

    @Operation(summary = "修改地址", description = "修改地址")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功修改地址")
    })
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook){
        log.info("修改地址：{}", addressBook);
        addressBookService.update(addressBook);
        return Result.success();
    }

    @Operation(summary = "根据ID删除地址", description = "根据ID删除地址")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "根据ID成功删除地址")
    })

    @DeleteMapping("/")
    public Result delete(Long id){
        log.info("根据ID删除地址：{}", id);
        addressBookService.deleteById(id);
        return Result.success();
    }
    @Operation(summary = "根据ID查询地址", description = "根据ID查询地址")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "根据ID成功查询地址")
    })
    @GetMapping("/{id}")
    public Result<AddressBook> getById(@PathVariable Long id){
        log.info("根据ID查询地址：{}", id);
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

}
